const path = require('path');
const webpack = require('webpack');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const isDev = process.env.NODE_ENV === 'development';

const config = {
    mode: process.env.NODE_ENV,
    entry: {
        app: ['babel-polyfill', path.join(__dirname, 'src/main.js')]
    },
    output: {
        filename: '[name].bundle.js',
        path: path.join(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(gif|jpg|jpeg|png|svg)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 1024,
                            name: 'img/[name].[hash:7].[ext]'
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': isDev ? '"development"' : '"production"'
            }
        }),
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            filename: path.join(__dirname, 'dist/index.html'),
            template: 'index.html'
        })
    ]
};

if (isDev) {
    config.devServer = {
        port: 8000,// 项目访问端口
        host: '127.0.0.1',// 配置为0.0.0.0可通过127.0.0.1或localhost访问
        overlay: {
            errors: true// 编译中遇到错误都会显示到网页中去
        },
        open: false,// 是否在项目启动时打开浏览器
        hot: true
    };
    config.plugins.push(
        // 下面两个插件都是用于hot: true热加载相关，目的是为了修改项目文件保存后不需要再刷新页面即可显示最新内容
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin()
    );
    config.devtool = 'cheap-module-eval-source-map';//
}


module.exports = config;
